﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SharingCenter.Web.User.CurrentProfile
{
    using SharingCenter.Web.Classes;

    public partial class MyPrivacy : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadPrivacySettings();
            }
            btnUpdatePrivacy.Click += new EventHandler(btnUpdatePrivacy_Click);
        }

        void LoadPrivacySettings()
        {
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    Sc_Privacy entry = cxt.Sc_Privacies.Single(d => d.UserId == Profile.UserId);
                    try
                    {
                        lstFriendRequest.Items.FindByValue(((int)entry.FriendRequestType).ToString()).Selected = true;
                        lstSearchable.SelectedIndex = Convert.ToBoolean(entry.IsSearchable) ? 0 : 1;
                    }
                    catch { }
                }
                catch
                {
                }
            }
            
            
        }

        void btnUpdatePrivacy_Click(object sender, EventArgs e)
        {
            // Save privacy settings
            Sc_Privacy entry = null;
            using (scDataContext cxt = new scDataContext())
            {
                try
                {
                    entry = cxt.Sc_Privacies.Single(d => d.UserId == Profile.UserId);
                }
                catch
                {
                    entry = new Sc_Privacy();
                    cxt.Sc_Privacies.InsertOnSubmit(entry);
                    entry.UserId = Profile.UserId;
                }

                entry.IsSearchable = Convert.ToBoolean(int.Parse(lstSearchable.SelectedValue));
                entry.FriendRequestType = (FriendRequestTypes)int.Parse(lstFriendRequest.SelectedValue);

                //WebsiteDataContext.Sc_Privacies.Attach(entry);
                cxt.SubmitChanges();
                PageUpdated = true;
            }
            
            RefreshPage();
        }
    }
}
